<?php
namespace App\Admin\Controllers;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Logics\ChannelLogic;

use Illuminate\Support\Facades\Storage;
use App\Logics\CategoryLogic;

class ChannelController extends BaseController{

    private function getInstance(){
        static $ob=null;
        if(is_null($ob)){
            $ob= new ChannelLogic();
        }
        return $ob;
    }
    //渠道列表
    public function index()
    {
        $page = (int)request("page", 1);
        $per_page = (int)request("per_page", 20);
        $cha_name = trim(request('cha_name'));
        $cha_id = (int)request('cha_id ',0);
        $cha_status = (int)request('cha_status',-1);

        $para=[
            'page'=>$page,
            'per_page'=>$per_page,
            'cha_name'=>$cha_name,
            'cha_id'=>$cha_id,
            'cha_status'=>$cha_status
        ];

        $ob= $this->getInstance();
        $arr=$ob->index($para);
        if($arr && is_string($arr)) die(json_error($arr));
        $content= view("admin.channel.index",$arr) ;
        $data=[
            'content'=>$content,
            'header'=>'渠道列表',
        ];
        return  $this->baseList($data);
    }

    //渠道添加
    public function add(Request $request){
        if($_POST){
            $cha_name=trim(request('cha_name'));
            $cha_describe=trim(request('cha_describe'));
            $app_id=trim(request('app_id'));
            $is_typ=(int)request('is_typ');
            $is_typ=$is_typ==1?1:2;

            $para=[
                'cha_name'=>$cha_name,
                'cha_describe'=>$cha_describe,
                'app_id'=>$app_id,
                'is_typ'=>$is_typ
            ];
            $ob=$this->getInstance();
            $data=$ob->add($para,$request);
            $this->result($data);
        }

        $content= view("admin.channel.add") ;
        $data=[
            'content'=>$content,
            'header'=>'渠道添加',
        ];
        return  $this->baseList($data);
    }

    public function edit(Request $request){
        if($_POST){
            $cha_name=trim(request('cha_name'));
            $cha_describe=trim(request('cha_describe'));
            $app_id=trim(request('app_id'));
            $is_typ=(int)request('is_typ');
            $cha_id= (int)request('cha_id');
            $cha_status=(int)request('cha_status');
            $is_typ==1?1:2;

            $para=[
                'cha_name'=>$cha_name,
                'cha_describe'=>$cha_describe,
                'app_id'=>$app_id,
                'is_typ'=>$is_typ,
                'cha_id'=>$cha_id,
                'cha_status'=>$cha_status
            ];
            $ob=$this->getInstance();
            $data=$ob->edit($para,$request);
            $this->result($data);
        }


        $cha_id=(int)request('cha_id');
        $data= ChannelLogic::getOne($cha_id);

        $url=config('sys.img_url').'/';
        $data['logo']=$data['logo']?$url.$data['logo']:$data['logo'];
        $content= view("admin.channel.edit",['data'=>$data]) ;
        $data=[
            'content'=>$content,
            'header'=>'渠道编辑',
        ];
        return  $this->baseList($data);
    }

    //删除渠道的图片
    public function delImg()
    {
        if($_POST){
            $cha_id=(int)request('cha_id');
            $ob=$this->getInstance();
            $data=$ob->deleteImg($cha_id);
            $this->result($data);
        }
        return json_error('失败');
    }

    //字段更新
    public function ajaxFieldsEdit(){
        $cha_id=(int)request('cha_id');
        $data=ChannelLogic::ajaxFieldsEdit($cha_id);
        $this->result($data);
    }



    public function xcxCategory(){

        return $this->category(1);
    }
    public function jdH5Category(){

        return $this->category(3);
    }
    public function xcxDlCategory(){

        return $this->category(4);
    }
    public function xcxStCategory(){

        return $this->category(5);
    }
    public function xcxAbsCategory(){

        return $this->category(6);
    }
    public function xcxAblCategory(){

        return $this->category(7);
    }
    public function hbH5Category(){

        return $this->category(8);
    }

    public function category($cha_id){



        $test=(int)request('test');
        $cha_id=(int)$cha_id;
        $ob=$this->getInstance();





        $data=$ob->category($cha_id);

        if($test==100){
            p($data);die;
        }

        if($data && is_string($data)) die(json_error($data));
        $content= view("admin.channel.category",$data) ;
        $data=[
            'content'=>$content,
            'header'=>'分类列表',
        ];
        return  $this->baseList($data);
    }

    public function editCategory(Request $request){
        if($_POST){
            $cate_name=trim(request('cate_name'));
            $cate_note=trim(request('cate_note'));
            $cate_code=trim(request('cate_code'));
            $cate_code_name=trim(request('cate_code_name'));
            $cate_hot_search=trim(request('cate_hot_search'));
            $pid=(int)request('pid',0);
            $cate_id=(int)request('cate_id');
            $p_sort=(int)request('p_sort',10);
            $is_del=(int)request('is_del',0);
            $is_hidden=(int)request('is_hidden',2);

            $para=[
                'cate_name'=>$cate_name,
                'cate_note'=>$cate_note,
                'cate_code'=>$cate_code,
                'cate_code_name'=>$cate_code_name,
                'cate_hot_search'=>$cate_hot_search,
                'pid'=>$pid,
                'cate_id'=>$cate_id,
                'p_sort'=>$p_sort,
                'is_del'=>$is_del,
                'is_hidden'=>$is_hidden
            ];
            $ob= $this->getInstance();
            $data=$ob->editCategory($para,$request);
             $this->result($data);
        }


        $id=(int)request('id');
        $ob= $this->getInstance();
        $data=$ob->getEditCategory($id);

        if($data && is_string($data)) die(json_error($data));
        $content= view("admin.channel.editCategory",$data) ;
        $data=[
            'content'=>$content,
            'header'=>'分类编辑',
        ];
        return  $this->baseList($data);
    }

    public function  addCategory(Request $request){
        if($_POST){
            $cha_id=(int)request('cha_id',0);
            $cate_name=trim(request('cate_name'));
            $cate_note=trim(request('cate_note'));
            $cate_code=trim(request('cate_code'));
            $cate_code_name=trim(request('cate_code_name'));
            $cate_hot_search=trim(request('cate_hot_search'));
            $pid=(int)request('pid',0);

            $para=[
                'cate_name'=>$cate_name,
                'cate_note'=>$cate_note,
                'cate_code'=>$cate_code,
                'cate_code_name'=>$cate_code_name,
                'cate_hot_search'=>$cate_hot_search,
                'pid'=>$pid,
                'cha_id'=>$cha_id
            ];
            $ob=$this->getInstance();
            $data=$ob->addCategory($para,$request);
            $this->result($data);
        }


        $cha_id=(int)request('cha_id');
        $ob= $this->getInstance();
        $data=$ob->getAddCategory($cha_id);
        if($data && is_string($data)) die(json_error($data));

        $content= view("admin.channel.addCategory",$data) ;
        $data=[
            'content'=>$content,
            'header'=>'分类添加',
        ];
        return  $this->baseList($data);
    }


    public function xcxGoods(){

        return $this->goods(1);
    }
    public function jdH5Goods(){

        return $this->goods(3);
    }
    public function xcxDlGoods(){

        return $this->goods(4);
    }
    public function xcxStGoods(){

        return $this->goods(5);
    }
    public function xcxAbsGoods(){

        return $this->goods(6);
    }
    public function xcxAblGoods(){

        return $this->goods(7);
    }
    public function hbH5Goods(){

        return $this->goods(8);
    }


    private function goods($cha_id=1){
        $cha_id=(int)$cha_id;
        $page = (int)request("page", 1);
        $per_page = (int)request("per_page", 20);
        $goods_name = trim(request('goods_name'));
        $goods_id = (int)request('goods_id');
        $is_putaway = (int)request('is_putaway','-1');

        $para=[
            'cha_id'=>$cha_id,
            'page'=>$page,
            'per_page'=>$per_page,
            'goods_name'=>$goods_name,
            'goods_id'=>$goods_id,
            'is_putaway'=>$is_putaway
        ];

        $ob=$this->getInstance();
        $data=$ob->goods($para);
        if($data && is_string($data)) die(json_error($data));
        $content= view("admin.channel.goods",$data) ;
        $data=[
            'content'=>$content,
            'header'=>'商品列表',
        ];
        return  $this->baseList($data);

    }

    //goodsDelete
    public function goodsDelete(){
        $id=trim(request('id'));
        $data=ChannelLogic::goodsDelete($id);
        $this->result($data);
    }

    //修改排序
    public function editSort(){
        $id=(int)request('id');
        $sort=(int)request('sort',20);

        $data=ChannelLogic::editSort($id,$sort);
        $this->result($data);
    }

    /**
     * 获取子类html
     */
    public function getChildHtml(){
        $id=(int)request('id');

        $ob= $this->getInstance();
        $html=$ob->getChildHtml($id);


        if($html){
            die(json_success('ok',['html'=>$html]));
        }
        die(json_error('获取失败'));
    }


}